﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Cailiao
{
    /// <summary>
    /// NewPage.xaml 的交互逻辑
    /// </summary>
    public partial class NewPage : Page
    {
        ViewModel.MaterialViewModel vm;
        Model.DBContext dBContext = new Model.DBContext();
        public NewPage()
        {
            InitializeComponent();
            vm = DataContext as ViewModel.MaterialViewModel;

            vm.depList = dBContext.DB.Queryable<Model.Depment>().ToList();

            //初始化
            vm.CurrBill = new Model.MaterialBill() { Id=Comm.BillNoGenerate.GenerateShenQingNO()};
            
        }

        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_Save_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                if (string.IsNullOrEmpty(vm.CurrBill.ItemName))
                {
                    Growl.WarningGlobal("请输入材料名称！");
                    return;
                }
                if (vm.CurrBill.ItemCount <= 0)
                {
                    Growl.WarningGlobal("请输入数量！");
                    return;
                }

                vm.CurrBill.CreateTime = DateTime.Now;
                vm.CurrBill.State = "待审核";


                vm.CurrBill.UserId = Comm.Temp.LoginUser.Id;
                vm.CurrBill.UserName = Comm.Temp.LoginUser.Name;

                dBContext.DB.Insertable<Model.MaterialBill>(vm.CurrBill).ExecuteCommand();

                Comm.BillNoGenerate.ShenqingNoAdd();

                Growl.SuccessGlobal("保存成功！");
            }
            catch (Exception ex)
            {

                Growl.ErrorGlobal(ex.Message);
            }
          
        }
    }
}
